package com.lcxw.hxzbapi.controller.shortvideocontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbapi.entity.requestBean.AddSvReq;
import com.lcxw.hxzbapi.entity.requestBean.GetSvListReq;
import com.lcxw.hxzbapi.entity.requestBean.LikeSvReq;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbcommon.common.ObsUtil;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/shortVideo")
@Slf4j
public class ShortVideoController {

    @Resource
    ShortVideoRealization shortVideoRealization;

    private ObsUtil obsUtil = new ObsUtil();

    //短视频资源上传接口
    @PostMapping("/resource/upload")
    public Response SvResUpload(@RequestParam("file") MultipartFile file) {

        String originalFilename = file.getOriginalFilename();
        String fileName = "shortVideo/" + originalFilename;
        String url;
        try {
            url = obsUtil.uploadResrouce(fileName, file.getBytes());
            if (null == url) {
                return Response.error(ErrorCode.EXTERNAL_ERROR, "上传到obs失败");
            }
        } catch (Exception e) {
            log.error("上传文件{}失败，原因为获取文件的字节码异常", originalFilename);
            return Response.error(ErrorCode.INVALID_PARAM, "上传文件失败，原因为获取文件的字节码异常");
        }
        return Response.ok(url);
    }

    //上传一个短视频接口
    @PostMapping("/add")
    public Response addSv(@RequestBody AddSvReq addSvReq) {
        return shortVideoRealization.addSv(addSvReq);
    }

    //删除一条短视频
    @PostMapping("/delete")
    public Response deleteSv(@RequestBody JSONObject requestBody) {
        long userId = Long.parseLong(requestBody.getString("userId"));
        Integer svId = requestBody.getInteger("svId");
        return shortVideoRealization.deleteSv(userId, svId);
    }

    //获取短视频列表的接口
    @PostMapping("/getList")
    public Response getSvList(@RequestBody GetSvListReq requestBody) {
        return shortVideoRealization.getSvList(requestBody);
    }

    //获取短视频列表的接口
    @PostMapping("/get/mine")
    public Response getMySvList(@RequestBody GetSvListReq requestBody) {
        return shortVideoRealization.getMySvList(requestBody);
    }

    //获取一条短视频接口
    @PostMapping("/getOne")
    public Response getOneSv(@RequestBody JSONObject requestBody) {
        int svId = requestBody.getIntValue("svId");
        return shortVideoRealization.getOneSv(svId);
    }

    //短视频点赞接口
    @PostMapping("/like")
    public Response likeSv(@RequestBody LikeSvReq likeSvReq) {
        return shortVideoRealization.likeSv(likeSvReq);
    }

    //取消短视频点赞接口
    @PostMapping("/unlike")
    public Response unlikeSv(@RequestBody LikeSvReq likeSvReq) {
        return shortVideoRealization.unlikeSv(likeSvReq);
    }


}
